public class Sore {

    //插入排序
    public static void insertSort(int[] array){
        for(int i = 1; i < array.length ;i++){
            int j = i-1;
            int tmp = array[i];
            while(j >= 0) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
                j--;
            }
            array[j+1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            shell(array,gap);
            gap = gap/2;
        }
        shell(array,gap);
    }
    public static void shell(int[] array, int gap){
        for(int i = gap ; i<array.length ;i++){
            int j = i - gap;
            int tmp = array[i];
            for(;j >= 0;j-= gap){
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    // 选择排序
    public static void selectSort1(int[] array){
        for (int i = 0; i < array.length; i++) {
            int mindex = i;
            int j = i+1;
            for(;j< array.length;j++){
                if(array[mindex]>array[j]){
                    mindex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[mindex];
            array[mindex] = tmp;
        }
    }
    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            int maxIndex = left;
            int minIndex = left;
            for (int i = left+1; i <= right; i++) {
                if(array[maxIndex] < array[i]){
                    maxIndex = i;
                }
                if(array[minIndex] > array[i]){
                    minIndex = i;
                }
            }
            swap(array,left,minIndex);
            if(left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

        private static void swap(int[] array ,int i ,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    // 堆排序
    public static void heapSort(int[] array){
        creatHeap(array);
        int len = array.length-1;
        while(len > 0){
            swap(array,0,len);
            shiftDown(array,0,len);
            len--;
        }
    }

    private static void creatHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    private static void shiftDown(int[] array,int parent,int len) {
        int child = parent*2+1;
        while(child < len){
            if(child+1<len &&array[child+1] > array[child]) {
                child++;
            }
            if(array[parent] < array[child]) {
                swap(array,parent,child);
            }
            parent = child;
            child = parent*2+1;
        }
    }
}
